Skip to content

Handle complex calculator results#47506

Merged
MuyuanMS merged 1 commit into
microsoft:mainfrom
MardSilva:BugFix-43937
May 28, 2026
Merged

Handle complex calculator results#47506
MuyuanMS merged 1 commit into
microsoft:mainfrom
MardSilva:BugFix-43937

Conversation

@MardSilva
Copy link
Copy Markdown
Contributor

Summary of the Pull Request

Return a friendly calculator error when Mages evaluates an expression to a complex number instead of letting decimal conversion throw.

This fixes the PowerToys Run Calculator result for expressions such as sqrt(-1) by detecting System.Numerics.Complex results before decimal conversion and showing a localized error message instead.

Fixes #43937

PR Checklist

Detailed Description of the Pull Request / Additional comments

The Calculator plugin previously passed complex results from Mages into Convert.ToDecimal, which caused an exception for expressions like sqrt(-1).

This PR updates the calculator result transformation logic to detect System.Numerics.Complex and return a localized user-facing error message: Complex numbers are not supported.

It also updates calculator query tests to cover both direct keyword and global query behavior.

Validation Steps Performed

  • Added unit test coverage for =sqrt(-1) returning Complex numbers are not supported.
  • Added unit test coverage for global query sqrt(-1) returning no result instead of surfacing an unhandled exception.
  • Ran git diff --check.
  • Attempted local build/test with the PowerToys build scripts, but local validation was blocked by Visual Studio/VC tooling configuration issues unrelated to this change: PlatformToolsetVersion resolves to an empty value during restore/build.

Return a friendly calculator error when Mages evaluates an expression to a complex number instead of letting decimal conversion throw.

Fixes microsoft#43937
@michaeljolley michaeljolley added the Product-Command Palette Refers to the Command Palette utility label Apr 30, 2026
@michaeljolley
Copy link
Copy Markdown
Contributor

Same here @MardSilva. Can you confirm a build/test?

@MardSilva
Copy link
Copy Markdown
Contributor Author

I updated my local dev environment according to the dev docs and validated this PR locally.

Validation passed:

  • tools\build\build-essentials.cmd
  • tools\build\build.cmd -Path src\modules\launcher\Plugins\Microsoft.PowerToys.Run.Plugin.Calculator.UnitTest
  • vstest.console.exe for Microsoft.PowerToys.Run.Plugin.Calculator.UnitTest: 362/362 passed

@michaeljolley
Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Copy Markdown
Collaborator

@daverayment daverayment left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks sound, but there's only a single test case at the moment. Although it tests the specific case referred to in the original issue, it would be useful to expand coverage to handle other cases and representations like sqrt(-4), (-1)^0.5 and log(-1).

@michaeljolley michaeljolley added Product-PowerToys Run Improved app launch PT Run (Win+R) Window and removed Product-Command Palette Refers to the Command Palette utility labels May 15, 2026
@MuyuanMS MuyuanMS merged commit c46083d into microsoft:main May 28, 2026
11 checks passed
LegendaryBlair pushed a commit that referenced this pull request May 29, 2026
## Summary of the Pull Request

Return a friendly calculator error when Mages evaluates an expression to
a complex number instead of letting decimal conversion throw.

This fixes the PowerToys Run Calculator result for expressions such as
`sqrt(-1)` by detecting `System.Numerics.Complex` results before decimal
conversion and showing a localized error message instead.

Fixes #43937

## PR Checklist

- [x] Closes: #43937
- [ ] **Communication:** I've discussed this with core contributors
already. If the work hasn't been agreed, this work might be rejected
- [x] **Tests:** Added/updated and all pass
- [x] **Localization:** All end-user-facing strings can be localized
- [x] **Dev docs:** Added/updated
- [x] **New binaries:** Added on the required places
- [x] [JSON for
signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json)
for new binaries
- [x] [WXS for
installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs)
for new binaries and localization folder
- [x] [YML for CI
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml)
for new test projects
- [x] [YML for signed
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml)
- [x] **Documentation updated:** Not required for this bug fix.

## Detailed Description of the Pull Request / Additional comments

The Calculator plugin previously passed complex results from Mages into
`Convert.ToDecimal`, which caused an exception for expressions like
`sqrt(-1)`.

This PR updates the calculator result transformation logic to detect
`System.Numerics.Complex` and return a localized user-facing error
message: `Complex numbers are not supported`.

It also updates calculator query tests to cover both direct keyword and
global query behavior.

## Validation Steps Performed

- Added unit test coverage for `=sqrt(-1)` returning `Complex numbers
are not supported`.
- Added unit test coverage for global query `sqrt(-1)` returning no
result instead of surfacing an unhandled exception.
- Ran `git diff --check`.
- Attempted local build/test with the PowerToys build scripts, but local
validation was blocked by Visual Studio/VC tooling configuration issues
unrelated to this change: `PlatformToolsetVersion` resolves to an empty
value during restore/build.

(cherry picked from commit c46083d)
@LegendaryBlair LegendaryBlair added this to the PowerToys 0.100 milestone Jun 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

0.100 Product-PowerToys Run Improved app launch PT Run (Win+R) Window

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unhandled exception when evaluating sqrt(-1) in PowerToys Run Calculator

5 participants